﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Drawing.Printing;

namespace CrystalReportPrintLibrary
{
    public class PrintClass
    {
        /// <summary>
        /// 水晶报表打印
        /// </summary>
        /// <param name="OperateType">操作类型</param>
        /// <param name="FileName">报表文件路径</param>
        /// <param name="oParameters">参数</param>
        /// <param name="PrinterName">打印机</param>
        public void CrystalReportPrint(OperateType oOperateType, string FileName, IList<Parameter> oParameters, string PrinterName = null)
        {
            try
            {

                ReportDocument oRptDoc = new ReportDocument();
                if (string.IsNullOrEmpty(FileName))
                    throw new Exception("文件路径有问题！");

                oRptDoc.Load(FileName);

                Log.Logger.Writer(FileName + " 报表加载成功！");

                LogonInfo oLogonInfo = new LogonInfo();

                oRptDoc = ConnectLogon(oRptDoc, oLogonInfo);
                Log.Logger.Writer("数据库连接成功！");

                foreach (Parameter oPar in oParameters)
                {
                    bool Flag = false;
                    for (int i = 0; i < oRptDoc.ParameterFields.Count; i++)
                    {
                        if (oRptDoc.ParameterFields[i].Name == oPar.ParameterName)
                        {
                            Flag = true;
                            break;
                        }
                    }
                    if (Flag)
                        oRptDoc.SetParameterValue(oPar.ParameterName, oPar.ParemeterValue);
                    else
                        throw new Exception(oPar.ParameterName + " 参数不存在于报表中，请检查");
                }
                Log.Logger.Writer("参数加载成功！");
                if (oOperateType == OperateType.Print)
                {
                    Log.Logger.Writer("开始打印！");                    
                    if (!String.IsNullOrEmpty(PrinterName))
                    {
                        PrintDocument Printer = new PrintDocument();
                        Printer.PrinterSettings.PrinterName = PrinterName;
                        oRptDoc.PrintOptions.PrinterName = Printer.PrinterSettings.PrinterName;
                    }
                    //要打印的份数、是否逐份打印页面、起始页和结束页
                    oRptDoc.PrintToPrinter(1, false, 0, 0);
                    Log.Logger.Writer("打印完成！");
                }
                else
                {
                    CrystalReportPrintViewer oViewer = new CrystalReportPrintViewer();
                    oViewer.Text = FileName;
                    oViewer.orpt = oRptDoc;
                    oViewer.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        /// <summary>
        /// 设置登陆参数
        /// </summary>
        /// <param name="Report"></param>
        /// <param name="oLogonInfo"></param>
        /// <returns></returns>
        private ReportDocument ConnectLogon(ReportDocument Report, LogonInfo oLogonInfo)
        {
            DataSourceConnections dataSourceConnections = Report.DataSourceConnections;
            IConnectionInfo oConnectInfo = dataSourceConnections[0];
            oConnectInfo.SetConnection(oLogonInfo.Server, oLogonInfo.DataBase, oLogonInfo.UserID, oLogonInfo.Password);
            return Report;
        }
    }
    public enum OperateType
    {
        Print = 1,
        Perview = 2
    };
    /// <summary>
    /// 登陆信息
    /// </summary>
    internal class LogonInfo
    {
        public LogonInfo()
        {
            _Server = ConfigurationManager.AppSettings["DataSource"].ToString();
            _Password = ConfigurationManager.AppSettings["Password"].ToString();
            _UserID = ConfigurationManager.AppSettings["UserID"].ToString();
            _DataBase = ConfigurationManager.AppSettings["InitialCatalog"].ToString();
        }
        private string _Server;
        /// <summary>
        /// 服务器
        /// </summary>
        public string Server
        {
            get { return _Server; }
            set { _Server = value; }
        }
        private string _UserID;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }
        private string _Password;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        private string _DataBase;
        /// <summary>
        /// 数据库
        /// </summary>
        public string DataBase
        {
            get { return _DataBase; }
            set { _DataBase = value; }
        }
    }
    /// <summary>
    /// 报表参数
    /// </summary>
    public class Parameter
    {
        private string _ParameterName;
        /// <summary>
        /// 参数名
        /// </summary>
        public string ParameterName
        {
            get { return _ParameterName; }
            set { _ParameterName = value; }
        }
        private string _ParemeterValue;
        /// <summary>
        /// 参数值 
        /// </summary>
        public string ParemeterValue
        {
            get { return _ParemeterValue; }
            set { _ParemeterValue = value; }
        }
    }
}
